<?php
namespace  app\lib;

use yii;
use app\service\LoginService;
use Lcobucci\JWT\Parser;

trait Base
{
    public function currentUser()
    {
        $jwt = $this->getToken();
        $token = (new Parser())->parse($jwt);
        $user_id = $token->getClaim('id');
        $loginService = new LoginService();

        $userInfo = $loginService->getUser($user_id);
        //当前用户对应的员工id
        if ($userInfo['role'] != 6) {
            $staffInfo = $loginService->getStaffInfo($userInfo['staff_id']);
            unset($staffInfo['id']);
            $staffInfo['zh_name'] = $staffInfo['name'];
            $arr = array_merge($userInfo, $staffInfo);
        } else {
            $arr = $userInfo;
        }
        return $arr;
    }

    public function getToken()
    {
        $jwt = yii::$app->request->cookies->get('jwt');
        $jwt2 = yii::$app->request->headers->get('authorization');
        if ($jwt) {
            return (string)$jwt;
        }
        if ($jwt2) {
            return (string)$jwt2;
        }
        return false;
    }
}